<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

//验证码的类
use Gregwar\Captcha\CaptchaBuilder;
class ZhuceController extends Controller
{
    //
    public function index()
    {
        $list=DB::table('type')->get();
        $config=DB::table('config')->get();
        return view("home.zhuce",['list'=>$list,'config'=>$config]);
    }
    public function create()
    {
        $list=DB::table('type')->get();
        $config=DB::table('config')->get();
    	return view("home.zhuce",['list'=>$list,'config'=>$config]);
    }
    public function store(Request $request)
    {
        $mycode = session()->get('milkcaptcha');
        if($mycode != $request->input('authcode')){
            return back()->with('msg','验证码错误！');
        }
        $user=DB::table('user')->where('username','=',$request->input('username'))->count();
        if($user==1){
            return back()->with('msg','用户名已存在！');
        }
   		$data = $request->only('username','email');
        $data['pass'] = md5($request->pass);
   		$id = DB::table("user")->insertGetId($data);
   		if($id>0){
   			return redirect("home/login");
   		}
    }
    public function capch($tmp)
    {
    	//生成验证码图片的Builder对象，配置相应属性   c
    	$builder = new CaptchaBuilder;
    	//可以设置图片宽高及字体
    	$builder->build($width = 100, $height = 40, $font = null);
    	//获取验证码的内容
    	$phrase = $builder->getPhrase();
    	//把内容存入session
    	session()->flash("milkcaptcha",$phrase);
    	return response($builder->output())->header("content-type","image/jpeg");
    }

}
